Public Class Plan

    Private idP As Integer
    Private cantActual As Double

    Public Sub New()
    End Sub

    Public Sub New(ByVal id As Integer, ByVal pactu As Double)
        Me.propActu = pactu
        Me.propId = id
    End Sub

    
    Public Sub crearRubro(ByVal prubro As String, ByVal pporcentaje As String, ByVal ptipo As Boolean, ByVal pextra As Boolean)
        Dim rub As Rubro = New Rubro()
        rub.validar(prubro, pporcentaje, ptipo, pextra, idP)
        If pextra Then
            rub.registrar()
        ElseIf cantActual + CType(pporcentaje, Double) > 100 Then
            Throw New ArgumentException("El porcentaje excede el 100%")
        Else
            'cantActual += CType(pporcentaje, Double)
            rub.registrar()
        End If
        Me.updatePlan()
    End Sub

    Public Sub crearRubro1(ByVal prubro As String, ByVal pporcentaje As Double, ByVal ptipo As String, ByVal pextra As String)
        Dim rub As Rubro = New Rubro
        rub.validar1(prubro, pporcentaje, ptipo, pextra, idP)
        'rub.validar(prubro, pporcentaje, ptipo, pextra, idP)
        'If pextra Then
        '    rub.registrar()
        'ElseIf cantActual + pporcentaje > 100 Then
        '    Throw New ArgumentException("El porcentaje excede el 100%")
        'Else
        '    'cantActual += pporcentaje
        rub.registrar()
        'End If
    End Sub


    Public Sub insertarPlan(ByVal pid As Integer)
        Me.idP = pid
        Me.cantActual = 0
        Me.insertPlan()
    End Sub

    Public Sub insertPlan()
        Dim dt As New accesoBD.TripleCDataSetTableAdapters.QueriesTableAdapter
        dt.crearPlan(Me.idP, Me.cantActual)
    End Sub

    Public Sub modificar(ByVal pid As Integer, ByVal prubro As String, ByVal pporcentaje As String, ByVal ptipo As Boolean, ByVal pextra As Boolean, ByVal pactual As Double, ByVal pinicial As Double)
        Dim rub As Rubro = New Rubro
        Dim objR As Rubro = Me.obtenerRubro(pid)
        rub.validar(prubro, pporcentaje, ptipo, pextra, idP)
        If pextra Then
            rub.update(pid, objR.getTipo())
        ElseIf ((pactual - pinicial) + CType(pporcentaje, Double)) > 100 Then
            Throw New ArgumentException("El porcentaje excede el 100%")
        Else
            'cantActual -= pinicial
            'cantActual += CType(pporcentaje, Double)
            rub.update(pid, objR.getTipo())
        End If
        Me.updatePlan()
    End Sub

    Public Sub delete(ByVal pid As Integer)
        Dim ds As New accesoBD.TripleCDataSetTableAdapters.QueriesTableAdapter
        ds.eliminarRubro(pid, idP)
        Me.updatePlan()
    End Sub

    Public Sub updatePlan()
        Dim da As DataTable = New accesoBD.TripleCDataSetTableAdapters.obtenerRubrosNoExtrasTableAdapter().GetData(idP)
        If da.Rows.Count() = 0 Then
            Dim dt As New accesoBD.TripleCDataSetTableAdapters.QueriesTableAdapter
            dt.guardarRubroO(idP)
        Else
            Dim dt As New accesoBD.TripleCDataSetTableAdapters.QueriesTableAdapter
            dt.modificarPlan(idP)
        End If
    End Sub

    Public Property propId()
        Get
            Return idP
        End Get
        Set(ByVal id)
            Me.idP = id
        End Set
    End Property

    Public Property propActu()
        Get
            Return cantActual
        End Get
        Set(ByVal actu)
            Me.cantActual = actu
        End Set
    End Property


    Public Function datosRubro() As DataTable
        Dim dt As DataTable = New accesoBD.TripleCDataSetTableAdapters.obtenerRubroTableAdapter().GetData(idP)
        Return dt
    End Function

    Public Function obtenerRubros() As DataTable
        Dim dt As DataTable = New accesoBD.TripleCDataSetTableAdapters.obtenerRubroTableAdapter().GetData(idP)
        Return dt
    End Function

    Public Function obtenerRubro(ByVal pidR As Integer) As Rubro
        Dim dt As DataTable = New accesoBD.TripleCDataSetTableAdapters.selectRubroTableAdapter().GetData(pidR)
        Dim idR As Integer = CInt(dt.Rows.Item(0).Item(0))
        Dim nombre As String = CType(dt.Rows.Item(0).Item(1), String)
        Dim porcentaje As Double = CType(dt.Rows.Item(0).Item(2), Double)
        Dim tipo As String = CType(dt.Rows.Item(0).Item(3), String)
        Dim extra As String = CType(dt.Rows.Item(0).Item(4), String)
        Dim objR As New Rubro(nombre, porcentaje, tipo, extra)
        Return objR
    End Function

End Class
